package com.fanhao.dllo.justforyou.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.justforyou.R;
import com.fanhao.dllo.justforyou.adapter.JokeMessageAdapter;
import com.fanhao.dllo.justforyou.view.SwipeRefreshLoadingLayout;
import com.fanhao.dllo.justforyou.entity.JokeMessageList;
import com.fanhao.dllo.justforyou.entity.JokeMessageResult;
import com.fanhao.dllo.justforyou.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;


/**
 * Created by dllo on 15/10/23.
 */
public class JokeMessageFragment extends Fragment implements SwipeRefreshLoadingLayout.OnRefreshListener, SwipeRefreshLoadingLayout.OnLoadListener {

    private RecyclerView recyclerView;
    private JokeMessageAdapter jokeMessageAdapter;

    private VolleySingleton volleySingleton;
    private SwipeRefreshLoadingLayout swipeRefreshLoadingLayout;


    private Context context;
    private int position;

//


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_joke_message, null);

        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView1);
        swipeRefreshLoadingLayout = (SwipeRefreshLoadingLayout) view.
                findViewById(R.id.swipeRefreshLoadingLayout);

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance();
        swipeRefreshLoadingLayout.setOnRefreshListener(this);
        swipeRefreshLoadingLayout.setOnLoadListener(this);

        initView();
        initData();
    }


    private void initView() {
        // 初始化recyclerView,并绑定布局管理器

        GridLayoutManager gm = new GridLayoutManager(context, 1);
        recyclerView.setLayoutManager(gm);

        //  初始化适配器
        jokeMessageAdapter = new JokeMessageAdapter(context);
        // 绑定适配器
        recyclerView.setAdapter(jokeMessageAdapter);


    }

    private void initData() {

        String url = "http://api.budejie.com/api/api_open.php?market=taobao&maxid=&" +
                "udid=359357052150266&a=list&appname=baisibudejie&c=data&os=4.3&client=android&" +
                "page=1&fresh=1&per=20&sub_flag=1&visiting=&type=29&time=week&" +
                "mac=50%3AFC%3A9F%3AFC%3A75%3A7E&ver=6.0.1";

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        swipeRefreshLoadingLayout.setRefreshing(false);
                        Gson gson = new Gson();
                        JokeMessageResult jmr = gson.fromJson(response,
                                JokeMessageResult.class);
                        if (jmr != null) {
                            List<JokeMessageList> list = jmr.getList();
                            Log.d("JokeMessageFragment", "list:" + list);
                            if (list != null && list.size() > 0) {
                                jokeMessageAdapter.addData(list);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        Log.d("JokeMessageFragment", "onErrorResponse() called with: " + "error = [" + error + "]");
                    }
                });

      volleySingleton.addRequest(request);

     }

    @Override
    public void onRefresh() {
        initData();
    }

    @Override
    public void onLoad() {
        loadData();
    }
//  private int page = 1;


    private void loadData() {
//        page++;


        String url = "http://api.budejie.com/api/api_open.php?market=taobao&maxid=&" +
                "udid=359357052150266&a=list&appname=baisibudejie&c=data&os=4.3&client=android&" +
                "page=" + 1 + "&fresh=1&per=20&sub_flag=1&visiting=&type=29&time=week&" +
                "mac=50%3AFC%3A9F%3AFC%3A75%3A7E&ver=6.0.1";

        StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        swipeRefreshLoadingLayout.setLoading(false);
                        Gson gson = new Gson();
                        JokeMessageResult jmr = gson.fromJson(response,
                                JokeMessageResult.class);
                        if (jmr != null) {
                            List<JokeMessageList> list = jmr.getList();
                            if (list != null && list.size() > 0) {
                                jokeMessageAdapter.loadData(list);
                                Log.d("list", list + "-------->");
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });
      volleySingleton.addRequest(request);
    }

}